#!/usr/bin/python

import os, string
from pprint import pprint

# in KB
CDROM   = 700 * 1024
DVD     = 4.5 * 1024 * 1024
max_bucket_size = CDROM
max_bucket_size = DVD

all_dirs = "*/"

lst_cmd = "du -s %s"%all_dirs

# make list of tuples: [(dir_size, dirname), *]
items = [ (int(size), name.strip()) for size, name in 
            [ line.split("\t", 1) for line in 
                os.popen(lst_cmd).readlines()
            ]
        ]

items.sort(reverse=True)
#pprint(items)

buckets = []
while len(items) > 0:
    bsize = 0
    bucket = []
    for item in items:
        if bsize + item[0] < max_bucket_size:
            bucket.append(item)
            bsize += item[0]
    if bsize == 0: break
    for item in bucket:
        items.remove(item)
    buckets.append((bsize, bucket))


# print report
print "bucketsize: ",
print "%.2f GB ="%(max_bucket_size/(1024.0*1024)),
print "%.2f MB ="%(max_bucket_size/1024.0),
print "%.2f KB"%(max_bucket_size)

bucket_no = 0
for bucket in buckets:
    bucket_no += 1
    print "bucket_%02i contains %02i items, %.2f%% full (=%i KB)"%(bucket_no, len(bucket[1]), 100*float(bucket[0])/max_bucket_size, bucket[0])
##    # print percentage + name
##    print "\n".join(["\t%.2f%%\t%s"%(100*float(size)/max_bucket_size, name) for size, name in bucket[1]])
    # print all names
    print "\t" + "\n\t".join([name for size, name in bucket[1]])
    
    
